package CaixaEletronico;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class EmprestimoDao {

	private Connection connection;

	public EmprestimoDao() {
    this.connection = new ConnectionFactory().getConnection();
  }
 

	public boolean insert(Emprestimo emprestimo) throws SQLException {
		String sql = "insert into tb_emprestimo " +
	            "(PARCELAS,VALOR_PARCELAS,ID,TOTAL)" +
	            " values (?,?,?,?);";
	            
	    try {
	        
	        PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
	        
	        stmt.setInt(1,emprestimo.getParcelas());
	        stmt.setDouble(2,emprestimo.getValor());
	        stmt.setInt(3,emprestimo.getId());
	        stmt.setDouble(4,emprestimo.getTotal());
	        stmt.execute();
	        stmt.close();
	        
	        
	    } catch (SQLException e) {
	        throw new RuntimeException(e);
	    }
		return true;
	}

	public ArrayList<Emprestimo> getAll(int id) throws SQLException {
		 
		ArrayList<Emprestimo> lista = new  ArrayList<Emprestimo>();
		
		Emprestimo emp = new Emprestimo();
		
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("Select from TB_EMPRESTIMO WHERE ="+id);
		  
		  ResultSet rs = stmt.executeQuery();
		   
		  while(rs.next()){
			  emp.setId_emprestimo(rs.getInt("ID_EMPRESTIMO"));
			  emp.setValor(rs.getDouble("VALORPARCELAS"));
			  emp.setParcelas(rs.getInt("PARCELAS"));
			  emp.setTotal(rs.getDouble("TOTAL"));
			  lista.add(emp);
		  }
		  rs.close();
		  stmt.close();
		  return lista;
	}

	
	public void disconnect() throws SQLException {
		this.connection.close();
		
	}

}

